package com.xdwanj.geoquiz.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.xdwanj.geoquiz.R;

public class CheatActivity extends AppCompatActivity {

    private final static String EXTRA_ANSWER_IS_TRUE = "com.xdwanj.geoquiz.answerIsTrue";
    public static final String EXTRA_ANSWER_SHOWN = "com.xdwanj.geoquiz.answerShow";

    private TextView answerTv;
    private Button showAnswerBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);

        //获取控件
        answerTv = (TextView) findViewById(R.id.answer_text_view);
        showAnswerBtn = (Button) findViewById(R.id.show_answer_button);

        //获取传过来的intent
        Intent intent = getIntent();
        //通过intent获取答案
        boolean answerIsTrue = intent.getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

        //答案区域设置事件
        showAnswerBtn.setOnClickListener(view -> {
            int answerText = 0;
            if (answerIsTrue) {
                answerText = R.string.true_button;
            } else {
                answerText = R.string.false_button;
            }
            answerTv.setText(answerText);
            //显示答案
            setAnswerShownResult(true);
        });
    }

    //获取一个Intent,给其他Activity用
    public static Intent newIntent(Context context, boolean answerIsTrue) {
        //放入数据,判断是否作弊的值
        return new Intent(context, CheatActivity.class)
                .putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
    }

    //设置答案区
    public void setAnswerShownResult(boolean isAnswerShown) {
        Intent intent = this.getIntent();
        intent.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);

        setResult(Activity.RESULT_OK, intent);
    }
}